(function () {
    window.BlockManager = class BlockManager {
        constructor () {
            this.gap = 70
            // 一块砖的体积
            this.blockHeight = 32
            this.blockWidth = 64
            // 最大行数与最大列数
            this.maxRow = Math.floor((game.canvas.height * 3 / 5  - this.gap) / this.blockHeight)
            this.maxCol = Math.floor((game.canvas.width - 2 * this.gap) / this.blockWidth)
            // 特别砖块数量
            this.special = 9
            this.canSpecial = true
            // 砖块数量
            this.blockNum = 0
            this.map = null
            this.realMap = null
        }
        createMap () {
            this.map = []
            this.realMap = []
            this.canSpecial = true
            for(let i = 0; i < this.maxRow; i++) {
                this.map[i] = []
                for(let j = 0; j < this.maxCol; j++) {
                    let type = this.canSpecial ? randomNum(0,100) : randomNum(0,5)
                    type = type === 60 ? 6 : type === 70 ? 7 : type === 90 ? 9 : type >= 98 ? 8 : randomNum(0,5)
                    if (type >=  6) {
                        this.special -- 
                        if (this.special === 0 ) {
                            this.canSpecial = false
                        }
                    }
                    this.map[i][j] = type
                }
            }
            this.map.forEach((item,i1) => {
                this.realMap[i1] = []
                item.forEach((subItem,i2) => {
                    if (subItem) {
                        this.blockNum++
                        this.realMap[i1][i2] = new Block({
                            row: i1 + 1,
                            col: i2 + 1,
                            color: subItem
                        })
                    } else {
                        this.realMap[i1][i2] = null
                    }
                })
            })
        }
        update () {

        }
        render () {
            this.realMap.forEach(item => {
                item.forEach(block => {
                    if (!block) return true
                    block.render()
                    block.update()
                })
            })
        }
    }
})()